package com.app.teaware.greendao;


import com.alibaba.fastjson.annotation.JSONField;
import com.app.teaware.bean.BoxInfoData;
import com.app.teaware.bean.BoxInfoDataDao;
import com.app.teaware.bean.BoxInfoListBean;
import com.app.teaware.bean.CompanyListBean;
import com.app.teaware.bean.CompanyListData;
import com.app.teaware.bean.CompanyListDataDao;
import com.app.teaware.bean.CraftListBean;
import com.app.teaware.bean.CraftListData;
import com.app.teaware.bean.CraftListDataDao;
import com.app.teaware.bean.DaoSession;
import com.app.teaware.bean.GoodsListBean;
import com.app.teaware.bean.GoodsListData;
import com.app.teaware.bean.GoodsListDataDao;
import com.app.teaware.bean.LocationListBean;
import com.app.teaware.bean.LocationListData;
import com.app.teaware.bean.LocationListDataDao;
import com.app.teaware.bean.SpeciesListBean;
import com.app.teaware.bean.SpeciesListData;
import com.app.teaware.bean.SpeciesListDataDao;
import com.app.teaware.bean.TrayInfoData;
import com.app.teaware.bean.TrayInfoDataDao;
import com.app.teaware.bean.TrayInfoListBean;
import com.app.teaware.bean.UnitListBean;
import com.app.teaware.bean.UnitListData;
import com.app.teaware.bean.UnitListDataDao;
import com.app.teaware.bean.UnitWholeListBean;
import com.app.teaware.bean.UnitWholeListData;
import com.app.teaware.bean.UnitWholeListDataDao;

import org.greenrobot.greendao.database.Database;

import java.util.List;

public class GreenDaoUtils {

    private static BoxInfoDataDao boxInfoDataDao = GreenDaoManager.getInstance().getmDaoSession().getBoxInfoDataDao();
    private static TrayInfoDataDao trayInfoDataDao = GreenDaoManager.getInstance().getmDaoSession().getTrayInfoDataDao();
    private static CompanyListDataDao customerDao = GreenDaoManager.getInstance().getmDaoSession().getCompanyListDataDao();
    private static CraftListDataDao craftDataDao = GreenDaoManager.getInstance().getmDaoSession().getCraftListDataDao();
    private static UnitListDataDao   unitListDataDao = GreenDaoManager.getInstance().getmDaoSession().getUnitListDataDao();
    private static UnitWholeListDataDao unitWholeDao = GreenDaoManager.getInstance().getmDaoSession().getUnitWholeListDataDao();
    private static GoodsListDataDao goodsListDao = GreenDaoManager.getInstance().getmDaoSession().getGoodsListDataDao();
    private static LocationListDataDao locationListDataDao =  GreenDaoManager.getInstance().getmDaoSession().getLocationListDataDao();
    private static SpeciesListDataDao speciesListDataDao =  GreenDaoManager.getInstance().getmDaoSession().getSpeciesListDataDao();
    public static final String TYPE_UNIT = "unit" //重量单位
            , TYPE_WHOLE_UNIT = "wholeUnit"   //托盘
            , TYPE_TRAY = "tray"   //托盘
            , TYPE_BOX = "box"   //纸箱
            , TYPE_CUSTOMER = "customer"   //客户
            , TYPE_CRAFT= "craft"   //工艺品
            , TYPE_GOODS= "goods"   //货品
            , TYPE_LOCATION= "location"   //楼层
            , TYPE_SPECIES= "species"   //货品分类
            ;


    public static void deleteAll(){
        boxInfoDataDao.deleteAll();
        trayInfoDataDao.deleteAll();
        customerDao.deleteAll();
        craftDataDao.deleteAll();
        unitListDataDao.deleteAll();
        unitWholeDao.deleteAll();
        goodsListDao.deleteAll();
        locationListDataDao.deleteAll();
        speciesListDataDao.deleteAll();
    }

    /**
     * 增加
     **/
    public static void unitBeanInsertData(UnitListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        unitListDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void unitWholeBeanInsertData(UnitWholeListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        unitWholeDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void boxBeanInsertData(BoxInfoListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        boxInfoDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void trayBeanInsertData(TrayInfoListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        trayInfoDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void customerBeanInsertData(CompanyListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        customerDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void craftBeanInsertData(CraftListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        craftDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void goodsBeanInsertData(GoodsListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        goodsListDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void   locationBeanInsertData(LocationListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        locationListDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void   speciesBeanInsertData(SpeciesListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        speciesListDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }














    /**
     * 查找
     **/

    public static LocationListBean getLocationListBean() {
        List<LocationListData> list = locationListDataDao.queryBuilder().build().list();
        LocationListBean locationListBean = new LocationListBean();
        for (LocationListData data : list) {
            locationListBean.getList().add(data);
        }
        return locationListBean;
    }

    public static UnitListBean getUnitListBean() {
        List<UnitListData> list = unitListDataDao.queryBuilder().build().list();
        UnitListBean unitListBean = new UnitListBean();
        for (UnitListData data : list) {
            unitListBean.getList().add(data);
        }
        return unitListBean;
    }

    public static UnitWholeListBean getUnitWholeListBean() {
        List<UnitWholeListData> list = unitWholeDao.queryBuilder().build().list();
        UnitWholeListBean unitListBean = new UnitWholeListBean();
        for (UnitWholeListData data : list) {
            unitListBean.getList().add(data);
        }
        return unitListBean;
    }

    public static TrayInfoListBean getTrayListBean() {
        List<TrayInfoData> list = trayInfoDataDao.queryBuilder().build().list();
        TrayInfoListBean stockInfoListBean = new TrayInfoListBean();
        for (TrayInfoData data : list) {
            stockInfoListBean.getList().add(data);
        }
        return stockInfoListBean;
    }

    public static BoxInfoListBean getBoxListBean() {
        List<BoxInfoData> list = boxInfoDataDao.queryBuilder().build().list();
        BoxInfoListBean boxInfoListBean = new BoxInfoListBean();
        for (BoxInfoData data : list) {
            boxInfoListBean.getList().add(data);
        }
        return boxInfoListBean;
    }

    public static CompanyListBean getCustomerListBean() {
        List<CompanyListData> list = customerDao.queryBuilder().build().list();
        CompanyListBean customerListBean = new CompanyListBean();
        for (CompanyListData data : list) {
            customerListBean.getList().add(data);
        }
        return customerListBean;
    }

    public static CraftListBean getCraftListBean() {
        List<CraftListData> list = craftDataDao.queryBuilder().build().list();
        CraftListBean craftListBean = new CraftListBean();
        for (CraftListData data : list) {
            craftListBean.getList().add(data);
        }
        return craftListBean;
    }

    public static SpeciesListBean getSpeciesListBean() {
        List<SpeciesListData> list = speciesListDataDao.queryBuilder().build().list();
        SpeciesListBean speciesListBean = new SpeciesListBean();
        for (SpeciesListData data : list) {
            speciesListBean.getList().add(data);
        }
        return speciesListBean;
    }



    public static String getCidByName(String cna) {
        String value ="";
        List<CraftListData> list = craftDataDao.queryBuilder().where(CraftListDataDao.Properties.Cna.eq(cna)).build().list();
        CraftListBean craftListBean = new CraftListBean();
        for (CraftListData data : list) {
            value = data.getCid();
        }
        return value;
    }

    public static String getGwtByGidFromGoodsTable(String gid) {
        String value ="";
        List<GoodsListData> list = goodsListDao.queryBuilder().where(GoodsListDataDao.Properties.Gid.eq(gid)).build().list();
        GoodsListBean goodsListBean = new GoodsListBean();
        for (GoodsListData data : list) {
            value = data.getGwt();
        }
        return value;
    }

    public static String getTidBySnaFromSpecies(String sna) {
        String value ="";
        List<SpeciesListData> list = speciesListDataDao.queryBuilder().where(SpeciesListDataDao.Properties.Sna.eq(sna)).build().list();
        for(SpeciesListData speciesListData : list){
            value+=speciesListData.getSid();
        }
        return value;
    }










    public static class EventDateChange {
        public static final String TYPE_ALL = "99";
        public String type;

        public EventDateChange(String type) {
            this.type = type;
        }
    }

    public interface GreenDaoInserCallBack {
        void onDataInsert(int count);
    }

    @JSONField(serialize = false)
    public static String getTypeName(String map_type) {
        switch (map_type) {
            case GreenDaoUtils.TYPE_UNIT:
                return "重量单位";
            case GreenDaoUtils.TYPE_TRAY:
                return "托盘";
            case GreenDaoUtils.TYPE_BOX:
                return "纸箱";
            case GreenDaoUtils.TYPE_CUSTOMER:
                return "客户";
            case GreenDaoUtils.TYPE_CRAFT:
                return "工艺品";
            case GreenDaoUtils.TYPE_GOODS:
                return "货品资料";
            default:
                return "未知";
        }
    }
}
